package com.xhsj.user.lsm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.lsm.entity.TbContractFile;
import com.xhsj.user.lsm.mapper.TbContractFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 合同文件表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-05-11
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbContractFile")
public class TbContractFileService extends BaseService<TbContractFileMapper, TbContractFile>{
    @Autowired
    private TbContractFileMapper dao;

    @Override
    //@CacheEvict(cacheNames="TbContractFile", allEntries=true)
    public boolean save(TbContractFile entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbContractFile getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbContractFile", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbContractFile> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbContractFile entity = JSON.parseObject(JSON.toJSONString(data), TbContractFile.class);
        Page<TbContractFile> page = new Page(currentPage,pageSize);
        QueryWrapper<TbContractFile> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbContractFile",sync = true)
    public List<TbContractFile> selectAll() {
        return super.selectAll();
    }

    //合同文件相关的数据
    public List<Map<String, Object>> contractFileList(String dataId,int type) {
       return dao.contractFileList(dataId,type);
    }

    //查询合同文件列表
    public List<TbContractFile> contractFileListByDataId(Serializable id) {
        return dao.contractFileListByDataId(id);
    }
}
